go to previous page   go to home page   go to next page

Answer:

No.

The non-empty cells of an ArrayList must contain object references (or null).


Wrapper Classes

To put an int into an ArrayList, put the int inside of an Integer object. Now the object can be put into an ArrayList. The following program builds a list of integers and then writes them out.

ArrayList of four Integers
import java.util.* ;
public class WrapperExample
{
  public static void main ( String[] args)
  {
    ArrayList<Integer> data = new ArrayList<Integer>();

    data.add( new Integer(1) );
    data.add( new Integer(3) );
    data.add( new Integer(17) );
    data.add( new Integer(29) );

    for ( Integer val : data )
      System.out.print( val + " " );

    System.out.println( );
  }
}

The program writes out:

1 3 17 29

The picture emphasizes that the ArrayList contains an array of object references, as always. It shows the ints each contained in a little box that represents the wrapper object.


QUESTION 19:

Would this statement work in the program?

data.add( 44 );